package com.mall.service.impl;

import com.mall.exception.ServiceException;
import com.mall.pojo.GoodsCategoryPo;
import com.mall.mapper.GoodsCategoryMapper;
import com.mall.service.GoodsCategoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mall.vo.Node;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 类目表 服务实现类
 * </p>
 *
 * @author zhangshiqi
 * @since 2020-09-13
 */
@Service
public class GoodsCategoryServiceImpl implements GoodsCategoryService {

    @Autowired
    private GoodsCategoryMapper goodsCategoryMapper;

    /**
     * 查询所有类目信息
     */
    @Override
    public List<Map<String, Object>> doFindObjects() {
        List<Map<String, Object>> list = goodsCategoryMapper.findObjects();
        if(list==null||list.size()==0)
            throw new ServiceException("没有部门信息");
        return list;

        System.out.println("你们都会用git啊");
        System.out.println("世奇，你提交个代码我看下");
        System.out.println("dvcd");
    }

    /**
     * 查询所有ztree节点信息
     */
    @Override
    public List<Node> doFindZTreeNodes() {
        List<Node> list = goodsCategoryMapper.findZTreeNodes();
        if(list==null||list.size()==0)
            throw new ServiceException("没有部门信息");
        return list;
    }

    /**
     * 保存类目信息
     */
    @Override
    public void doSaveObject(GoodsCategoryPo goodsCategoryPo) {
        //1.合法验证
        if(goodsCategoryPo==null)
            throw new ServiceException("保存对象不能为空");
        if(StringUtils.isEmpty(goodsCategoryPo.getName()))
            throw new ServiceException("类目名称不能为空");
        //2.保存数据
        int rows = goodsCategoryMapper.insert(goodsCategoryPo);
        if (rows==0)
            throw new ServiceException("类目信息添加失败");
    }

    /**
     * 删除类目信息
     */
    @Override
    public void doDeleteObject(Integer id) {
        //1.合法性验证
        if(id==null||id<=0)
            throw new ServiceException("数据不合法,id="+id);
        //2.执行删除操作
        //2.1判定此id对应的菜单是否有子元素
        int childCount=goodsCategoryMapper.getChildCount(id);
        if(childCount>0)
            throw new ServiceException("此元素有子元素，不允许删除");
        //2.2执行删除操作
        int rows=goodsCategoryMapper.deleteById(id);
        if(rows==0)
            throw new ServiceException("此信息可能已经不存在");
    }

    /**
     * 修改类目信息
     */
    @Override
    public void doUpdateObject(GoodsCategoryPo goodsCategoryPo) {
        //1.合法验证
        if(goodsCategoryPo==null)
            throw new ServiceException("保存对象不能为空");
        if(StringUtils.isEmpty(goodsCategoryPo.getName()))
            throw new ServiceException("类目名称不能为空");
        //2.修改类目信息
        int rows = goodsCategoryMapper.updateById(goodsCategoryPo);
        if (rows==0)
            throw new ServiceException("类目信息修改失败");
    }
}
